angular.module("shopping",[])
.config(["$stateProvider",function($stateProvider){
	$stateProvider.state("/shopping",{
		url:"/shopping",
		templateUrl:"html/shoppingcar/shopping.html",
		css:'html/shoppingcar/shopping.css',
		controller:"shoppingController"
	})
}]).controller("shoppingController",function($scope,$rootScope){
	// //点击加号
	// $scope.addcart = function(shop){
	// 	var boll = false;
	// 	for(var i = 0;i < $rootScope.cartData.length;i++){
	// 		if($rootScope.cartData[i]["id"] == shop['id']){
	// 			$rootScope.cartData[i].count++;
	// 			boll = true;
	// 			break;
	// 		}
	// 	}
	// 	if(!boll){
	// 		shop.count=1;
	// 		$rootScope.cartData.push(shop)
	// 	}
	// }
	// //点击减号
	// $scope.remove = function(shop){
	// 	for (var i = 0; i < $rootScope.cartData.length; i++) {
	// 		if($rootScope.cartData[i]["id"] == shop["id"]){
	// 			$rootScope.cartData[i]["count"]--
	// 			if($rootScope.cartData[i]["count"]==0){
	// 				$rootScope.cartData.splice(i,1);
	// 			}
	// 			break;
	// 		}
	// 	}
	// }
	// //单个商品数量
	// $scope.ss = function(shop){
	// 	for (var i = 0; i < $rootScope.cartData.length; i++) {
	// 		if($rootScope.cartData[i]["id"] == shop["id"]){
	// 			return $rootScope.cartData[i]["count"]
	// 			break;
	// 		}
	// 	}
	// }
	// $scope.total=function(){
	// 	var sum=0;
	// 	for(var i=0;i<$rootScope.cartData.length;i++){
	// 		sum+=($rootScope.cartData[i]['price']*$rootScope.cartData[i]['count']);					
	// 	}
	// 	return sum;	
	// }
	

	////////此处为调试代码

	//点击add;
	$scope.addcart = function(){
		this.data.count++;
	}

	//点击减少
	$scope.remove = function(data){
		if(this.data.count > 1){
			this.data.count--;
		}else if(confirm("是否删除?")){
			$rootScope.cartData.splice(data,1)
		}
	}

	//总价的方法
	$scope.total = function(){
		var total = 0;
		angular.forEach($rootScope.cartData,function(data){
			if(data.shopBuy){
				total += data.price*data.count;
			}
		})
		$scope.allChoose = true;
		for (var i = 0; i < $rootScope.cartData.length; i++) {
			if(!$rootScope.cartData[i].shopBuy){
				$scope.allChoose = false;
			}
		}
		$scope.allPrice = total;
		return total;
	}

	//全选框的判定
	$scope.allRight = function(m){
		if(m){
			for (var i = 0; i < $rootScope.cartData.length; i++) {
				$rootScope.cartData[i].shopBuy = true;
			}
		}else{
			for (var i = 0; i < $rootScope.cartData.length; i++) {
				$rootScope.cartData[i].shopBuy = false;
			}
		}
	}

	//选好了按钮变色
	$scope.buycolor = function(){
		for (var i = 0; i < $rootScope.cartData.length; i++) {
			if($rootScope.cartData[i].shopBuy){
				return "yellow";
			}else{
				return "";
			}
		}
	}
	
})